package com.xiaoyun.advicetemplate.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.xiaoyun.advicetemplate.annotation.ResultAdvice;
import com.xiaoyun.advicetemplate.entity.Order;
import com.xiaoyun.advicetemplate.vo.OrderEncrypt;
import com.xiaoyun.advicetemplate.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@Slf4j
public class OrderController {

    @ResultAdvice(outEncode = true)
    @GetMapping("/order/yes")
    public OrderEncrypt getOrderYes() {
        Order order = new Order();
        order.setId(2);
        order.setOrderCount(5);
        order.setOrderName("订单名称1");
        order.setOrderPrice(new BigDecimal("100").toString());
        OrderEncrypt orderEncrypt = new OrderEncrypt().build(order);
        return orderEncrypt;
    }

    @ResultAdvice(outEncode = true)
    @GetMapping("/order/yes01")
    public OrderEncrypt yes01(@RequestBody String requestData) {
        Order order = new Order();
        order.setId(2);
        order.setOrderCount(5);
        order.setOrderName("订单名称1");
        order.setOrderPrice(new BigDecimal("100").toString());
        OrderEncrypt orderEncrypt = new OrderEncrypt().build(order);
        return orderEncrypt;
    }

    @ResultAdvice(inDecode = true)
    @GetMapping("/order/encode")
    public OrderEncrypt encode(@RequestBody String requestData) {
        log.info("解密数据:{}", requestData);
        OrderEncrypt orderEncrypt = JSON.parseObject(requestData, OrderEncrypt.class);
        return orderEncrypt;
    }


    @GetMapping("/order/no")
    public Order getOrderNo() {
        Order order = new Order();
        order.setId(2);
        order.setOrderCount(50);
        order.setOrderName("订单名称2");
        order.setOrderPrice(new BigDecimal("200").toString());
        return order;
    }

    @GetMapping("/order/error")
    public Order error() {
        throw new RuntimeException("访问出错~");
    }
}
